package com.yx.cloud.activity;

import android.annotation.SuppressLint;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.aliyun.aligeniessp_1_0.models.GetDeviceBasicInfoResponseBody;
import com.google.gson.Gson;
import com.iot.base.bean.FirmwareInfo;
import com.iot.base.service.req.ConfigRecordSaveParam;
import com.iot.common.utils.LogUtils;
import com.iot.product.sweeper.config.SweeperProductConfig;
import com.iot.sdk.yugong.YuGongSdk;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.EquipmentmessageActivityBinding;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.utils.AliHttpRequest_Runnable;
import com.yx.cloud.utils.AnimationtUtils;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.LoadProgressDialog;
import com.yx.cloud.utils.LocationUtils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.StringUtils;
import com.yx.cloud.utils.ThreadUtils;
import com.yx.cloud.utils.ToastUtil;
import com.iot.product.sweeper.bean.Robot;
import com.iot.common.utils.Event;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

/**
 * 创建时间 : 2018/12/26.
 * 创建人 :
 * 功能描述 :设备信息
 */
public class EquipmentMessageActivity extends BaseActivity implements View.OnClickListener {
    private final static String TAG = "EquipmentMessageActivity";
    private EquipmentmessageActivityBinding binding;
    private String mac = "";
    private LoadProgressDialog loadProgressDialog;

    @Override
    public void onCreate() {
        binding = EquipmentmessageActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initWidget();
    }


    @SuppressLint("LongLogTag")
    private void initWidget() {
        loadProgressDialog = new LoadProgressDialog(this);

        Robot robot = AppContext.get().getCurrentDevice();
        JSONObject productConfig = AppContext.getConfig();

        binding.rlHostMac.setVisibility(productConfig.optBoolean(ProductConfigConstants.hostMac) ? View.VISIBLE : View.GONE);
        binding.rlHostFirmwareVersion.setVisibility(productConfig.optBoolean(ProductConfigConstants.hostMac) ? View.VISIBLE : View.GONE);
        binding.rlRobotsettingBsVersion.setVisibility(productConfig.optBoolean(ProductConfigConstants.hasBaseStationVersion) ? View.VISIBLE : View.GONE);
        binding.rlRobotsettingUuid.setVisibility(productConfig.optBoolean(ProductConfigConstants.hasUUID) ? View.VISIBLE : View.GONE);
        binding.rlRobotsettingSn.setVisibility(productConfig.optBoolean(ProductConfigConstants.hasSN) ? View.VISIBLE : View.GONE);
        binding.rlUploadLog.setVisibility(productConfig.optBoolean(ProductConfigConstants.uploadLog) ? View.VISIBLE : View.GONE);//因为陀螺仪机器目前不支持日志上传

        String mBaseStationVersion = robot.getFirmwareInfo().getBaseStationVersion();
        if (TextUtils.isEmpty(mBaseStationVersion)) {
            binding.tvBaseStationVersionNumber.setText(R.string.robotsetting_Bs_Version_toast);
        } else {
            binding.tvBaseStationVersionNumber.setText(mBaseStationVersion);
        }

        FirmwareInfo firmwareInfo = robot.getFirmwareInfo();
        Log.d(TAG, " : firmwareInfo : " + new Gson().toJson(firmwareInfo));
        binding.rlWifiSsid.setVisibility(productConfig.optBoolean(ProductConfigConstants.robotInfo_wifi) ? View.VISIBLE : View.GONE);
        if (!TextUtils.isEmpty(firmwareInfo.getSsid())) {
            binding.tvWifiName.setText(firmwareInfo.getSsid());
        }
        if (!TextUtils.isEmpty(firmwareInfo.getIp())) {
            binding.tvIpAddress.setText(firmwareInfo.getIp());
        }
        if (!TextUtils.isEmpty(firmwareInfo.getSn())) {
            binding.tvSnNumber.setText(firmwareInfo.getSn());
        }

        mac = firmwareInfo.getMac();
        binding.tvMacAddress.setText(mac);
        if (!TextUtils.isEmpty(firmwareInfo.getUuid())) {
            binding.tvUuidNumber.setText(firmwareInfo.getUuid());
        }
        if (!TextUtils.isEmpty(firmwareInfo.getCompileVerions())) {
            binding.tvFirmwareVersion.setText(firmwareInfo.getCompileVerions());
        }
        binding.tvDeviceID.setText(robot.getDeviceId());
        binding.ivEquipmentMessageReturn.setOnClickListener(this);
        binding.rlRobotsettingBsVersion.setOnClickListener(this);
        binding.rlUploadLog.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_equipmentMessageReturn:
                Intent_utils.backIntent(this);
                break;
            case R.id.rl_robotsetting_Bs_Version:
                //获取基站软件版本
                AnimationtUtils.setRotateAnimation(binding.ivBasestationversionRefresh, 600);
                countDownTimer.start();
                AppContext.getProductService().getBaseStationInfo();
                break;
            case R.id.rl_upload_log:
                loadProgressDialog.show();
                AppContext.getProductService().uploadLog();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MEG_ROBOT_BASESTATION:
                binding.ivBasestationversionRefresh.clearAnimation();
                //基站软件版本
                if (event.isResult()) {
                    String baseStationVersion = (String) event.getContent();
                    if (baseStationVersion == null || baseStationVersion.equals("")) {
                        ToastUtil.show(EquipmentMessageActivity.this, getString(R.string.robotsetting_Bs_Version_toast), Toast.LENGTH_LONG);
                    } else {
                        binding.tvBaseStationVersionNumber.setText(baseStationVersion);
                    }
                }
                break;
            case Event.MESSAGE_TYPE_GET_DEVICE_BASIC_INFO:
                //X5基站固件信息
                if (event.isResult()) {
                    GetDeviceBasicInfoResponseBody.GetDeviceBasicInfoResponseBodyResult body = (GetDeviceBasicInfoResponseBody.GetDeviceBasicInfoResponseBodyResult) event.getContent();
//                    LogUtils.i("基站 body ：" + new Gson().toJson(body));
                    if (!TextUtils.isEmpty(body.getMac())) {
                        binding.tvHostMac.setText(body.getMac());
                    }
                    if (!TextUtils.isEmpty(body.getFirmwareVersion())) {
                        binding.tvHostFirmwareVersion.setText(body.getFirmwareVersion());
                    }
                }
                break;
            case Event.MESSAGE_TYPE_LOG_UPLOAD:
                //上传日志
                if (loadProgressDialog != null) {
                    loadProgressDialog.dismiss();
                }
                break;
        }
    }

    private final CountDownTimer countDownTimer = new CountDownTimer(15000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
        }

        @Override
        public void onFinish() {
            binding.ivBasestationversionRefresh.clearAnimation();
            ToastUtil.show(EquipmentMessageActivity.this, getString(R.string.robotsetting_Bs_Version_toast), Toast.LENGTH_LONG);
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
